package com.qianfeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Drug;
import com.qianfeng.entity.DrugRegistration;
import com.qianfeng.entity.Registration;
import com.qianfeng.mapper.DrugMapper;
import com.qianfeng.mapper.SendDrugMapper;
import com.qianfeng.service.RegistrationService;
import com.qianfeng.service.SendDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
@Transactional
public class SendDrugServiceImpl implements SendDrugService {
    @Autowired
    private SendDrugMapper sendDrugMapper;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private RegistrationService registrationService;


    @Override
    public void addSendDrug(DrugRegistration drugRegistration) {
        System.out.println("-----------");
        drugRegistration.setDreTime(new Date());
        System.out.println(drugRegistration);
        sendDrugMapper.addSendDrug(drugRegistration);
        Drug drug = drugMapper.getDrugDetailByDrId(drugRegistration.getDrId());
        //System.out.println(drug);
        //drug.setDrId(drugRegistration.getDrId());
        drug.setDrNumber(drug.getDrNumber()-drugRegistration.getDreNumber());
        drugMapper.updateDrug(drug);
    }

    @Override
    public Page getgetSendDrugPage(Integer pageNo, Integer pageSize) {
        int pageNo_=1;
        int pageSize_=2;
        if (pageNo!=null){
            pageNo_=pageNo;
        }
        if (pageSize!=null){
            pageSize_= pageSize;
        }
        Page page = new Page();
        PageHelper.startPage(pageNo_,pageSize_);
        List<DrugRegistration> drugRegistrations = sendDrugMapper.getgetSendDrugList();
        PageInfo<DrugRegistration> pageInfo = new PageInfo<>(drugRegistrations);
        page.setPageCount(pageInfo.getPages());
        page.setHasNext(pageInfo.isHasNextPage());
        List<DrugRegistration> list = pageInfo.getList();
        for (DrugRegistration drugRegistration : list) {
            Drug drugDetailByDrId = drugMapper.getDrugDetailByDrId(drugRegistration.getDrId());
            drugRegistration.setDrug(drugDetailByDrId);
            Registration registrationById = registrationService.getRegistrationById(drugRegistration.getrId());
            drugRegistration.setRegistration(registrationById);
        }
        page.setObj(list);
        return page;
    }

    @Override
    public List<DrugRegistration> getSendDrugListByRId(Integer rId) {
        List<DrugRegistration> sendDrugListByRId = sendDrugMapper.getSendDrugListByRId(rId);
        for (DrugRegistration drugRegistration : sendDrugListByRId) {
            Drug drugDetailByDrId = drugMapper.getDrugDetailByDrId(drugRegistration.getDrId());
            drugRegistration.setDrug(drugDetailByDrId);
            Registration registrationById = registrationService.getRegistrationById(drugRegistration.getrId());
            drugRegistration.setRegistration(registrationById);
        }
        return sendDrugListByRId;
    }

    @Override
    public void updateSendDrug30(DrugRegistration drugRegistration) {
        DrugRegistration DR = sendDrugMapper.getByDreId(drugRegistration.getDreId());
        if ((DR.getDreNumber()-DR.getDrePushNo())<30){
            drugRegistration.setDrePushNo(DR.getDreNumber());
            sendDrugMapper.updateSendDrug(drugRegistration);
        }else {
            drugRegistration.setDrePushNo(DR.getDrePushNo()+30);
            sendDrugMapper.updateSendDrug(drugRegistration);
        }

    }

    @Override
    public void updateSendDrug1(DrugRegistration drugRegistration) {
        DrugRegistration DR = sendDrugMapper.getByDreId(drugRegistration.getDreId());

        if ((DR.getDreNumber()-DR.getDrePushNo())<1){
            drugRegistration.setDrePushNo(DR.getDreNumber());
            sendDrugMapper.updateSendDrug(drugRegistration);
        }else {
            drugRegistration.setDrePushNo(DR.getDrePushNo()+1);
            sendDrugMapper.updateSendDrug(drugRegistration);
        }
    }

    @Override
    public void updateSendDrug5(DrugRegistration drugRegistration) {
        DrugRegistration DR = sendDrugMapper.getByDreId(drugRegistration.getDreId());

        if ((DR.getDreNumber()-DR.getDrePushNo())<5){
            drugRegistration.setDrePushNo(DR.getDreNumber());
            sendDrugMapper.updateSendDrug(drugRegistration);
        }else {
            drugRegistration.setDrePushNo(DR.getDrePushNo()+5);
            sendDrugMapper.updateSendDrug(drugRegistration);
        }
    }

    @Override
    public void updateSendDrugAll(DrugRegistration drugRegistration) {
        DrugRegistration DR = sendDrugMapper.getByDreId(drugRegistration.getDreId());
        drugRegistration.setDrePushNo(DR.getDreNumber());
        sendDrugMapper.updateSendDrug(drugRegistration);
    }
}
